<template>
  <div class="tuijian">
    <div class="coent">
      <van-tree-select @click-item="handleData" height="94vh" :items="items" :active-id.sync="activeIds" :main-active-index.sync="activeIndex" />
    </div>
    <div style="margin-top: 2px" @click-item="handleGoToNext">
      <van-button type="info" block>下一步</van-button>
    </div>
  </div>
</template>

<script>
import { FileServerProxy } from '../../utils/api'
export default {
  data() {
    return {
      rowData: {},
      items: [],
      activeIds: [],
      activeIndex: 0,
      tableData: [],
      indexData: [],
      tableData1: [],
      ids: 0,
      code1: '',
      ifMapType: ''
    }
  },
  mounted() {
    if (ZWJSBridge) {
      ZWJSBridge.setTitle({
        title: '选择人员'
      })
        .then((res) => {})
        .catch((err) => {})
    }
  },
  methods: {
    async getData() {
      let params = {
        accessCode: '0a709d649c314903a72225edb8bc8c2a',
        cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000',
        params: {
          taskId: this.rowData.TASK_ID,
          direction: 1, //0是发送  1是打回
          token: window.localStorage.getItem('token')
        }
      }
      const res = await FileServerProxy(params)
      let datasendForm = JSON.parse(res.data.data)
      var ids = 0
      if (datasendForm.SendData && datasendForm.SendData.length > 0) {
        datasendForm.SendData.forEach((newItem) => {
          if (newItem.PTNID == '-1') {
            this.items.push({
              text: newItem.TEXT,
              WFNODEGUID: newItem.WFNODEGUID,
              dot: false,
              TNID: newItem.TNID,
              PTNID: newItem.PTNID,
              // 导航节点额外类名
              className: 'my-class',
              // 该导航下所有的可选项
              children: []
            })
          }
        })
        datasendForm.SendData.forEach((newItem) => {
          this.items.forEach((ites) => {
            if (ites.TNID == newItem.PTNID) {
              ites.children.push({
                text: newItem.USERNAME,
                id: this.ids,
                USERID: newItem.USERID,
                USERNAME: newItem.USERNAME,
                WFNODEGUID: newItem.WFNODEGUID,
                WFNODENAME: newItem.TEXT,
                TEXT: newItem.USERNAME,
                PTNID: ites.TNID,
                TNID: this.ids + 1,
                ISUSER: 1,
                SINGLEASSIGN: false,
                WFNODETYPE: newItem.WFNODETYPE
              })
            }
          })
        })
        console.log(this.items)
      } else {
        this.$toast.fail('为查询到要发送的人员信息')
        setTimeout(() => {
          this.$router.push({
            path: '/MyTodoList'
          })
        }, 500)
      }
    },
    //组合
    handleData(data) {
      // this.indexData.push(data)
      // console.log(this.indexData)
      if (this.indexData.length > 0) {
        let status = false
        this.activeIds.forEach((item) => {
          if (item != data.id) {
            status = true
          }
        })
        if (status) {
          this.indexData.push(data)
        }
        let datas = []
        this.activeIds.forEach((item) => {
          this.indexData.forEach((items) => {
            if (item == items.id) {
              datas.push(items)
            }
          })
        })
        this.indexData = datas
      } else {
        this.indexData.push(data)
      }
      console.log(this.indexData)
    },
    async handleGoToNext() {
      let plantask = {
        accessCode: 'c52bcf7cf3fb42bb9fbcd9b91e97d411',
        cityCode: window.localStorage.getItem('citycode') ? window.localStorage.getItem('citycode') : '441300000000',
        params: {
          taskId: this.rowData.TASK_ID,
          direction: 0,
          wfBoxGuid: '',
          actionText: '同意',
          taskName: '',
          wfGuid: this.xcfy,
          sendNodes: JSON.stringify(this.indexData),
          token: window.localStorage.getItem('token')
        }
      }
      const respl = await FileServerProxy(plantask)
      let data = JSON.parse(respl.data.data).Success
      if (data) {
        setTimeout(() => {
          this.$router.push({
            path: '/MyTodoList'
          })
        }, 500)
      }
    }
  },
  created() {
    this.rowData = JSON.parse(this.$route.query.data)
    this.getData()
  }
}
</script>
